<template>
  <div class="addGoodContainer">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加商品</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="back">返回</el-button>
      </div>
      <!--    添加商品表单数据-->
      <el-form ref="addForm" :model="addGoodForm" :rules="rules" label-width="120px">
        <el-form-item label="商品品牌">
          <el-select v-model="brand" value-key="id"  placeholder="请选择品牌">
            <el-option v-for="(item,index) in brandList" :key="index" :label="item.name" :value="item"/>
          </el-select>
        </el-form-item>
        <el-form-item label="商品分类">

          <el-select v-model="goodsType" value-key="id"  placeholder="请选择商品分类">
            <el-option v-for="(item,index) in goodsTypeList" :key="index" :label="item.name" :value="item"/>
          </el-select>

        </el-form-item>
        <el-form-item label="商品图片" prop="images">
          <el-upload
            action="/oss/img"
            :file-list="fileList"
            list-type="picture-card"
            name="object"
            :on-success="uploadSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"/>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImg" alt="">
          </el-dialog>
        </el-form-item>
        <el-form-item label="SKU名称" prop="name">
          <el-input v-model="addGoodForm.name" style="width: 300px"/>
        </el-form-item>
        <el-form-item
          label="价格（元）"
          prop="price"
        >
          <el-input v-model.number="addGoodForm.price" type="price" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="规格">
          <el-input v-model="addGoodForm.spec" style="width: 300px"/>
        </el-form-item>
        <el-form-item label="商品状态">
          <el-radio-group v-model="addGoodForm.status">
            <el-radio label="1">正常</el-radio>
            <el-radio label="2">下架</el-radio>
            <el-radio label="3">删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('addForm')">添加</el-button>
          <el-button @click="back">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {getBranList} from '@/api/goodBrand'
import {addGoodInfo, getGoodTypeList} from '@/api/goods'

export default {
  name: 'AddGoodInfo',
  data() {
    var validatePass = (rule, value, callback) => {

      console.log('validatePass', value)
      if (!this.addGoodForm.images) {
        callback(new Error('请上传图片'))
      } else {
        callback()
      }
    }
    return {
      //  添加表单数据
      addGoodForm: {},
      // 品牌信息
      brandList: [],
      brand:{id:'',name:''}
      ,
      // 商品分类
      goodsTypeList: [],
      goodsType:{id:'',name:''},
      tt: null,
      // 上传图片数据
      fileList: [],
      dialogVisible: false,
      dialogImg: '',
      rules: {
        price: [
          {required: true, message: '价格不能为空'},
          {type: 'number', message: '价格必须为数字值'}
        ],
        name: [
          {required: true, message: '商品名称不能为空'}
        ],
        images: [
          {required: true, trigger: 'blur', validator: validatePass}
        ]
      }
    }
  },
  watch: {
    fileList(newFileList, oldFileList) {
      this.addGoodForm.images = newFileList.length === 0 ? null : newFileList.map(item => item.url).join(',')
    }
  },
  created() {
    this.loadBrand();
    this.loadGoodsType();
  },
  methods: {
    // 加载品牌信息
    loadBrand() {
      getBranList().then(res => this.brandList = res.data)
    },
    // 加载分类信息
    loadGoodsType() {
      getGoodTypeList().then(res=>this.goodsTypeList = res.data)
    },

    back() {
      this.$store.dispatch('tagsView/delVisitedView', this.$route)
      this.$router.push('/goods/goodinfo')
    },
    //  图片上传成功时的回调
    uploadSuccess(file, fileList) {
      this.fileList.push({url: fileList.response.data})
    },
    // 移除上传图片数据
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogVisible = true
      this.dialogImg = file.url
    },
    //  提交表单
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        console.info("brand",this.brand)
        if (valid) {
          addGoodInfo({
            brandName: this.brand.name,
            brandId: this.brand.id,
            categoryName: this.goodsType.name,
            categoryId: this.goodsType.id,
            images: this.addGoodForm.images,
            name: this.addGoodForm.name,
            price: this.addGoodForm.price,
            spec: this.addGoodForm.spec * 100,
            status: this.addGoodForm.status
          }).then(() => {
            this.$message.success('添加成功')
            this.back()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.addGoodContainer {
  padding: 20px;
}
</style>
